import cv2 as cv

cap = cv.VideoCapture(0)
fps = 30
size = (int(cap.get(cv.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv.VideoWriter('assets/my_video.avi', cv.VideoWriter_fourcc(*'MJPG'), fps, size)

success, frame = cap.read()
numFramesRemaining = 10 * fps - 1
while numFramesRemaining > 0:
    if frame is not None:
        videoWriter.write(frame)
    success, frame = cap.read()
    numFramesRemaining -= 1
    cv.imshow("camera", frame)
    cv.waitKey(1)